﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;
using System.IO;
using SMALLERP;
using XJLSC.PM;

namespace XJLSC.SO
{
    public partial class WLMXQ : Form
    {
        private readonly DataBase db = new DataBase();
        private System.Data.SqlClient.SqlDataReader sdr1;
        public WLMXQ()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = null;
            strSql = "SELECT TD001 订单单别,TD002 订单单号,TD003 订单序号,TD004 品号,TD005 品名,TD006 规格,TD008 订单数量,TD009 已交数量,TD008-TD009 未交数量,TD013 预计交货日 FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 WHERE TD021='Y' AND TC003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TC003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112)";

            //textBox6.Text = strSql;

            try
            {
                DataTable dt;
                dataGridView1.DataSource = "";
                dataGridView1.Refresh();

                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                dataGridView1.DataSource = dt;


                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.dataGridView1.AllowUserToAddRows = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strSql = null;
            strSql = "DELETE FROM DDMXQ";

            //MessageBox.Show(strSql,"提示");
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {
                    //MessageBox.Show("保存失败！", "软体提示");
                    //return;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }

            progressBar1.Visible = true;
            progressBar1.Maximum = dataGridView1.Rows.Count - 1;//设置最大长度值
            progressBar1.Value = 0;//设置当前值
            progressBar1.Step = 1;//设置没次增长多少
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                progressBar1.Value += progressBar1.Step;
                strSql = " INSERT INTO DDMXQ SELECT '" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["订单单别"].Index].Value.ToString() + "','" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["订单单号"].Index].Value.ToString() + "','" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["订单序号"].Index].Value.ToString() + "','" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["品号"].Index].Value.ToString() + "',ID,MB002,MB003," + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["未交数量"].Index].Value.ToString() + "," + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["未交数量"].Index].Value.ToString() + "*MD006/MD007,'' FROM Loadbom3('" + this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["品号"].Index].Value.ToString() + "') LEFT JOIN INVMB ON ID=MB001";

                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            progressBar1.Visible = false;
            MessageBox.Show("生成完毕！", "软体提示");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strSql = null;
            strSql = "SELECT A.ID 品号,MB002 品名,MB003 规格,INVMB.UDF01 老编码,A.XQSL 需求数量,ISNULL(B.TB009,0) 请购数量,ISNULL(C.TA015,0) 下单数量,A.XQSL-ISNULL(B.TB009,0)-ISNULL(C.TA015,0) AS 套料数量 FROM (SELECT ID,SUM(XQSL) XQSL FROM DDMXQ LEFT JOIN INVMB ON ID=MB001 GROUP BY ID) A LEFT JOIN (SELECT TB004,SUM(TB009) TB009 FROM PURTB LEFT JOIN PURTA ON TB001=TA001 AND TB002=TA002 WHERE TA003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) GROUP BY TB004) B ON A.ID=B.TB004 ";
            strSql += " LEFT JOIN (SELECT TA006,SUM(TA015) TA015 FROM MOCTA WHERE TA003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) GROUP BY TA006) C ON A.ID=C.TA006 LEFT JOIN INVMB ON ID=MB001";

            //textBox6.Text = strSql;

            try
            {
                DataTable dt;
                dataGridView1.DataSource = "";
                dataGridView1.Refresh();

                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                dataGridView1.DataSource = dt;


                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.dataGridView1.AllowUserToAddRows = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }

            double sum = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                sum += double.Parse(this.dataGridView1.Rows[i].Cells["需求数量"].Value.ToString());
            }
            textBox1.Text = sum.ToString();

            double sum1 = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                sum1 += double.Parse(this.dataGridView1.Rows[i].Cells["请购数量"].Value.ToString());
            }
            textBox2.Text = sum1.ToString();

            double sum2 = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                sum2 += double.Parse(this.dataGridView1.Rows[i].Cells["套料数量"].Value.ToString());
            }
            textBox3.Text = sum2.ToString();

            double sum3 = 0;
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                sum3 += double.Parse(this.dataGridView1.Rows[i].Cells["下单数量"].Value.ToString());
            }
            textBox4.Text = sum3.ToString();

        }

        private void button4_Click(object sender, EventArgs e)
        {

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (dataGridView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string x1 = "";
            string x2 = "";
            double x3 = 0.00;
            string x4 = "";
            string x5 = "";
            x1 = dataGridView1["品号", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            x2 = dateTimePicker1.Value.ToShortDateString();
            x3 = Convert.ToDouble(this.dataGridView1.CurrentCell.Value.ToString());
            x5 = dateTimePicker2.Value.ToShortDateString();

            if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["需求数量"].Index)
            {
                x4 = "查看需求数量";

            }
            else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["请购数量"].Index)
            {
                x4 = "查看请购数量";

            }
            else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["下单数量"].Index)
            {
                x4 = "查看下单数量";

            }
            else
            {
                x4 = "其他";
                MessageBox.Show("本列无明细数据", "软件提示");
            }

            if (x4 != "其他")
            {
                MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                CHAXUN f = new CHAXUN();
                f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                f.Show(this);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
